package com.example.user.myhjjf_app.view;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.blankj.utilcode.util.ActivityUtils;
import com.example.user.myhjjf_app.HJJF_Url;
import com.example.user.myhjjf_app.R;
import com.example.user.myhjjf_app.model.Model_Regional;
import com.example.user.myhjjf_app.model.Model_Tagging;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.Response;

import java.util.HashMap;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class Activity_gjx extends AppCompatActivity {
    Model_Tagging.ValueBean.ArrayBean.DetailsBean detailsBean;
    @BindView(R.id.btn_back)
    LinearLayout btnBack;
    @BindView(R.id.tx_title)
    TextView txTitle;
    @BindView(R.id.ed_mc)
    EditText edMc;
    @BindView(R.id.ed_zjdmc)
    EditText edZjdmc;
    @BindView(R.id.ed_zggl)
    EditText edZggl;
    @BindView(R.id.ed_zjdgl)
    EditText edZjdgl;
    @BindView(R.id.ed_cdxs1)
    EditText edCdxs1;
    @BindView(R.id.ed_cdxs2)
    EditText edCdxs2;
    @BindView(R.id.ed_jd)
    EditText edJd;
    @BindView(R.id.ed_wd)
    EditText edWd;
    @BindView(R.id.bt_add_zd)
    Button btAddZd;
    String type;
    String zoneId,departId;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_gjx);
        ButterKnife.bind(this);
        Bundle mbundle = getIntent().getExtras();
        type = mbundle.getString("type");
        txTitle.setText("光交箱编辑");
        if (type.equals("edit")){
            detailsBean = (Model_Tagging.ValueBean.ArrayBean.DetailsBean) mbundle.getSerializable("detailsBean");
            edMc.setText(detailsBean.getName());
            edZjdmc.setText(detailsBean.getZjdName());
            edZggl.setText(String.valueOf(detailsBean.getZggqCount()));
            edZjdgl.setText(String.valueOf(detailsBean.getZjdCount()));
            edCdxs1.setText(String.valueOf(detailsBean.getRedCount()));
            edCdxs2.setText(String.valueOf(detailsBean.getYellowCount()));
            edJd.setText(String.valueOf(detailsBean.getLng()));
            edWd.setText(String.valueOf(detailsBean.getLat()));
        }else {
            edJd.setText(mbundle.getString("lng"));
            edWd.setText(mbundle.getString("lat"));
        }

    }

    @OnClick({R.id.btn_back, R.id.bt_add_zd})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.btn_back:
                finish();
                break;
            case R.id.bt_add_zd:
                String name = edMc.getText().toString();
                String zjdName = edZjdmc.getText().toString();
                String lng = edJd.getText().toString();
                String lat = edWd.getText().toString();
                String zggqCount = edZggl.getText().toString();
                String redCount = edCdxs1.getText().toString();
                String yellowCount = edCdxs2.getText().toString();
                String zjdCount = edZjdgl.getText().toString();
                zoneId = Model_Regional.getRegional().getZoneId();
                departId = Model_Regional.getRegional().getDepartId();
                if (name.equals("") || zjdName.equals("") || lng.equals("") || lat.equals("") || zggqCount.equals("") || yellowCount.equals("") || redCount.equals("")) {
                    Toast.makeText(this, "除描述外其他为必填项~~", Toast.LENGTH_SHORT).show();
                } else {
                    doAdd(name, zjdName, lng, lat, zggqCount, zjdCount, redCount, yellowCount, zoneId, departId);
                }
                break;
        }
    }
    private void doAdd(String name,String zjdName,String lng,String lat,String zggqCount,String redCount,String yellowCount,String zjdCount,String zoneId,String departId){
        String url = HJJF_Url.getHJJF_Url().login("api/data/gjx_add.do");
        HashMap map = new HashMap();
        if (type.equals("edit")) {
            url = HJJF_Url.getHJJF_Url().login("api/data/gjx_edit.do");
            map.put("id",detailsBean.getId());
        }
        map.put("name", name);
        map.put("zoneId", zoneId);
        map.put("zjdName", zjdName);
        map.put("lng", lng);
        map.put("lat", lat);
        map.put("zggqCount", zggqCount);
        map.put("redCount", redCount);
        map.put("distance", "0");
        map.put("departId", departId);
        map.put("yellowCount", yellowCount);
        map.put("zjdCount", zjdCount);
        OkGo.<String>post(url)
                .tag(this)
                .params(map)
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(Response<String> response) {
                        setResult(1);
                        finish();
                    }
                });
    }
}
